<?php
/**
 * 功能: 推广位
 * User: WD
 * Date: 2018/9/10 0010
 * Time: 9:31
 */

namespace app\admin\controller;

use app\base\controller\Power;
use think\Db;

class Pid extends Power{
    protected $beforeActionList = [
        'first'=> ['except'=>'index'],

    ];
    protected function first(){
        $result = $this->testing();
    }
    /**
     * 推广位列表
     */
    public function index(){
        $pid = Db::name('pid')->order('id desc')->paginate(10)->each(function($item,$key){
            $item['phone'] = Db::name('user')->where('id',$item['user_id'])->value('phone');
            return $item;
        });
        $count = Db::name('pid')->count();
        $this->assign([
            'pid'=>$pid,
            'count'=>$count,
            'empty'=>'<tr class="text-c empty"><td colspan="5">暂无数据</td></tr>'
        ]);
       return view('');

    }

    /**
     * 推广位 搜索
     */
    public function searchPid(){
        $change = intval(input('change'));
        $common = trim(input('common'));
        $user = intval(input('user'));
        if($change==1){
            //pid分配情况
            if($user==1){
                //已分配
                $pid = Db::name('pid')
                    ->where('user_id','>',0)
                    ->order('id desc')
                    ->paginate(10,false,['query'=>request()->param()])->each(function($item,$key){
                        $item['phone'] = Db::name('user')->where('id',$item['user_id'])->value('phone');
                        return $item;
                    });

                $count = Db::name('pid')->where('user_id','>',0)->count();
            }else{
                $pid = Db::name('pid')
                    ->where('user_id',0)
                    ->order('id desc')
                    ->paginate(10,false,['query'=>request()->param()])->each(function($item,$key){
                        $item['phone'] = Db::name('user')->where('id',$item['user_id'])->value('phone');
                        return $item;
                    });
                $count = Db::name('pid') ->where('user_id',0)->count();

            }

        }elseif($change==2){
            //用户电话号/推广位
            $pid = Db::name('pid')
               ->alias('p')
                ->join('user u','u.id = p.user_id ')
                ->where('u.phone','like','%'.$common.'%')
                ->whereOr('p.adzone_pid','like','%'.$common.'%')
                ->order('id desc')
                ->field('u.phone,p.*')
                ->paginate(10,false,['query'=>request()->param()]);
            $count = Db::name('pid')->alias('p')
                ->join('user u','u.id = p.user_id ')
                ->where('u.phone','like','%'.$common.'%')
                ->whereOr('p.adzone_pid','like','%'.$common.'%')->count();

        }else{
            return json(['msg'=>'非法请求！','ok'=>false]);
        }
        if(!empty($pid[0])){
            $this->assign([
                'pid'=>$pid,
                'count'=>$count
            ]);
            return $this->fetch('pid:index',['change'=>$change,'common'=>$common,'user'=>$user]);
        }else{
            return json(['msg'=>'没有符合条件的结果！','ok'=>false]);
        }

    }

    /**
     * 推广位添加
     */
    public function pid_add(){
        $file = request()->file('txt');
         /*echo "<pre>";
          print_r($file);die;*/
        $info = $file->validate(['size'=>10485760,'ext'=>'txt'])->move(ROOT_PATH . 'public' . DS . 'pid');
        if ($info) {
            $img =   $info->getSaveName();;
            $img1 = str_replace("\\","/",$img);
            $json_string = file_get_contents('pid/'.$img1);

            // 用参数true把JSON字符串强制转成PHP数组
            $data = json_decode($json_string, true);
            $result = $data['data']['pagelist'];
           /* echo "<pre>";
            print_r($result);
            die;*/
            $arr = $arrs = [];
            $i = 0;
            foreach($result as $k=>$v){
                if(empty($v['sitename'])){
                    $arr['adzone_id'] = $v['adzoneid'];
                    $arr['adzone_pid'] = $v['adzonePid'];
                    $check = Db::name('pid')
                        ->where('adzone_id',$arr['adzone_id'])
                        ->find();
                    if($check){
                        $res =  Db::name('pid')->where('adzone_id',$arr['adzone_id'])->update($arr);
                        if($res){
                            return json(['msg'=>'插入成功','ok'=>true]);
                        }else{
                            return json(['msg'=>'插入失败','ok'=>false]);
                        }

                    }else{
                        $arrs[]  =  $arr ;
                    }
                    $i++;
                }

            }
           /* echo "<pre>";
            print_r($arr);die;*/
            $res = Db::name('pid')->insertAll($arrs);
            if($res){
                $error = $i-$res;
                return json(['msg'=>"总{$i}条，成功{$res}条，失败{$error}条。",'ok'=>true]);
            }else{
                return json(['msg'=>'插入失败','ok'=>false]);
            }
        }


    }

}